{extend name="public/container"}
{block name="content"}
<div id="myApp">
    <div class="douxue-filter padding-20 flex flex-between" style="margin-bottom:20px;">
        <el-form :inline="true" :model="where" size="mini" class="flex">
            <div class="flex flex-between">
              <el-form-item>
                <el-select
                    size="small"
                    v-model="where.status"
                    placeholder="是否显示"
                    :clearable=true>
                    <el-option label="显示" :value="1"></el-option>
                    <el-option label="隐藏" :value="2"></el-option>
                </el-select>
              </el-form-item>

              <el-form-item>
                <el-input size="small" v-model="where.title" placeholder="请输入关键字">
                    <el-button slot="append" size="small" type="primary" @click="doLoadRows(1)" icon="el-icon-search"></el-button>
                </el-input>
              </el-form-item>
            </div>
        </el-form>
        <el-button type="primary" size="mini" @click="doAdd()" icon="el-icon-plus">新增</el-button>
    </div>

    <div class="padding-10">
        <el-table
            :data="rows"
            style="width: 100%"
            size="mini">
            <el-table-column
                prop="id"
                label="ID"
                width="80"
                align="left">
            </el-table-column>
            <el-table-column
                prop="title"
                label="名称"
                align="left">
            </el-table-column>
            <el-table-column
                prop="icon"
                label="ICON"
                width="120"
                align="left">
                <template slot-scope="scope">
                    <el-image
                        style="width: 70px; height: 70px"
                        :src="scope.row.icon"
                        :preview-src-list="[scope.row.icon]">
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column
                prop="sort"
                label="排序"
                width="120"
                cell-click="cell"
                align="left">
            </el-table-column>
            <el-table-column
                prop="status"
                label="是否显示"
                width="120"
                align="left">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.status"
                        active-color="#ff6a6c"
                        inactive-color="#999"
                        :active-value="1"
                        :inactive-value="2"
                        @change="doSetStatus(scope.row)">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column
                label=""
                width="120"
                align="left">
                <template slot-scope="scope">
					<el-link type="warning" size="mini" :underline="false" @click="doRoutes(scope.row.id)">线路</el-link>
                    <el-link type="primary" size="mini" :underline="false" @click="doAdd(scope.row)">编辑</el-link>
                    <el-link type="danger" size="mini" :underline="false" @click="doDelete(scope.row.id)">删除</el-link>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                small
                :hide-on-single-page="true"
                layout="prev, pager, next"
                :total="rowsCount"
                :page-size="where.limit"
                @current-change="doLoadRows">
            </el-pagination>
        </div>
    </div>

    <!-- 添加/编辑   -->
    <el-dialog title="添加/编辑" :visible.sync="dialogFormVisible" @close='closeDialog'>
        <el-form :model="formData" :rules="rules" ref="formData" label-width="80px" >
            <el-form-item label="名称" prop="title">
                <el-input v-model="formData.title" placeholder="名称" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="图标" prop="cover">
                <el-upload
                        class="avatar-uploader"

                        action="/admin/upload/image"
                        accept="image/jpeg,image/png,image/jpg"
                        :show-file-list="false"
                        :on-success="uploadCoverSuccess"
                        :before-upload="beforeAvatarUpload">
                    <img v-if="formData.icon" :src="formData.icon" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    <div slot="tip" class="el-upload__tip">尺寸建议为70×70或等比例，格式png/jpg且不超过1MB</div>
                </el-upload>
            </el-form-item>
            <el-form-item label="排序">
                <el-input v-model="formData.sort" placeholder="搜索名称" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="是否启用">
                <el-select v-model="formData.status" placeholder="请选择">
                    <el-option
                        v-for="item in statusOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit('formData')">确 定</el-button>
                <el-button @click="dialogFormVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
	
	<!--线路推荐-->
	<el-dialog title="线路推荐" width="70%" fullscreen :visible.sync="showDoRoutes" @close="closeRoutes">
        <div class="flex">
            <div class="left" style="width:40%; padding: 20px; border-right: 1px solid #f2f2f2;">
                <div style="margin-bottom: 20px;">
                    <div style="color: #bb0000; font-weight: bold;margin-bottom: 10px;">所有线路</div>
                    <div class="flex">
                        <div style="width: 100px;margin-right: 15px;">
                            <el-select
                                size="mini"
                                v-model="routesWhere.route_type"
                                placeholder="线路类型"
                                :clearable=true>
                                <el-option label="长线" :value="1"></el-option>
                                <el-option label="短线" :value="2"></el-option>
                                <el-option label="周边游" :value="3"></el-option>
                            </el-select>
                        </div>
                        <div style="flex:1">
                            <el-input size="mini" type="text" v-model="routesWhere.route_name" placeholder="请输入线路名称">
                                <template slot="append">
                                    <el-button size="mini" icon="el-icon-search" @click="getRoutesList(1)"></el-button>
                                </template>
                            </el-input>
                        </div>
                    </div>
                </div>
                <div style="height: 550px; overflow-y: scroll;">
                    <el-table
                        :data="routes"
                        v-loading="routeloading"
                        element-loading-text="加载中"
                        element-loading-spinner="el-icon-loading"
                        style="width: 100%"
                        >
                        <el-table-column
                          prop="route_name"
                          label="线路名称">
                        </el-table-column>

                        <el-table-column
                          prop="route_type_text"
                          width="100"
                          label="线路类型">
                        </el-table-column>

                        <el-table-column
                            label="操作"
                            align="right"
                            width="80">
                            <template slot-scope="scope">
                                <el-link type="warning" size="mini" :underline="false" @click="addRelRoutes(scope.row.id)">加入</el-link>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <div class="pagination">
                    <el-pagination
                        small
                        :hide-on-single-page="true"
                        layout="prev, pager, next"
                        :total="routesCount"
                        :page-size="routesWhere.limit"
                        @current-change="getRoutesList">
                    </el-pagination>
                </div>
            </div>

            <div class="right" style="width:60%; padding: 20px;">
                <div style="margin-bottom: 20px;">
                    <div style="color: #bb0000; font-weight: bold;margin-bottom: 10px;">已加入推荐</div>
                    <div class="flex">
                        <div style="width: 100px;margin-right: 15px;">
                            <el-select
                                size="mini"
                                v-model="relRoutesWhere.route_type"
                                placeholder="线路类型"
                                :clearable=true>
                                <el-option label="长线" :value="1"></el-option>
                                <el-option label="短线" :value="2"></el-option>
                                <el-option label="周边游" :value="3"></el-option>
                            </el-select>
                        </div>
                        <div style="flex:1">
                            <el-input size="mini" type="text" v-model="relRoutesWhere.route_name" placeholder="请输入线路名称">
                                <template slot="append">
                                    <el-button size="mini" icon="el-icon-search" @click="getRelRoutesList(1)"></el-button>
                                </template>
                            </el-input>
                        </div>
                    </div>
                </div>
                <div style="height: 550px; overflow-y: scroll;">
            		<el-table
            		    :data="relRoutes"
                        v-loading="relRouteloading"
                        element-loading-text="加载中"
                        element-loading-spinner="el-icon-loading"
            		    style="width: 100%"
            		    >
            		    <el-table-column
            		      prop="route_name"
            		      label="线路名称">
            		    </el-table-column>

                        <el-table-column
                          prop="route_type_text"
                          width="100"
                          label="线路类型">
                        </el-table-column>

                        <el-table-column
                            prop="sort"
                            label="排序"
                            width="100">
                            <template slot-scope="scope">
                                <el-input style="width:80px" v-model="scope.row.sort" size="mini" :controls="false" @change="changeRoutesSort(scope.row)" :min="0" :max="9999"></el-input>
                            </template>
                        </el-table-column>

            			<el-table-column
                            prop=""
                            label="操作"
                            width="100">
                            <template slot-scope="scope">
                                <el-link type="danger" size="mini" :underline="false" @click="rmRelRoutes(scope.row.route_id)">移除</el-link>
                            </template>
                        </el-table-column>
            		</el-table>
                </div>
                <div class="pagination">
                    <el-pagination
                        small
                        :hide-on-single-page="true"
                        layout="prev, pager, next"
                        :total="relRoutesCount"
                        :page-size="relRoutesWhere.limit"
                        @current-change="getRelRoutesList">
                    </el-pagination>
                </div>
            </div>
        </div>
	</el-dialog>
</div>
{/block}
{block name="script"}
<script>
    var my = new Vue({
        el: "#myApp",
        data(){
            return {
                dialogFormVisible: false,
                formData: {
                    id: 0,
                    title: '',
                    icon: '',
                    status: 1,
                    type: 1
                },
                rules: {
                    title: [
                        { required: true, message: '搜索名称不能为空', trigger: 'blur' },
                    ]
                },
                rowsCount: 0,
                rows: [],
                where:{
                    page:1,
                    limit: pageSize,
                    type: 1,
                    status:'',
                    title:'',
                },
                statusOptions: [
                    {value: 1, label: '是'},
                    {value: 2, label: '否'},
                ],
				showDoRoutes: false,
				routes: [],
                routesCount: 0,
                routesWhere: {
                    route_name: '',
                    route_type: '',
                    limit: 6,
                    page: 1,
                    rel: 1
                },
                recomandId: 0,
                routeloading: false,
                relRouteloading: false,
                relRoutes: [],
                relRoutesCount: 0,
                relRoutesWhere: {
                    route_name: '',
                    route_type: '',
                    limit: 6,
                    page: 1,
                    recomand_id: 0
                }
            }
        },
        methods: {
            uploadCoverSuccess(response, file, fileList) {
                if (response.code != 200) {
                    return this.$message.error(response.msg);
                } else {
                    this.formData.icon = response.url
                    return this.$message.success(response.msg);
                }
            }
            ,beforeAvatarUpload(file) {
                const isLt2M = file.size / 1024 / 1024 < 5;
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 5MB!');
                }
                return isLt2M;
            }
            ,doLoadRows (pn) {
                this.where.page = pn || 1
                axios.get(Url({a:'get_list' , q:this.where})).then(res => {
                    this.rows = res.data.data
                    this.rowsCount = res.data.count
                })
            }
            // 设置状态
            ,doSetStatus (row) {
                axios.get(Url({a:'set_status',q:{status:row.status,id:row.id}})).then(res => {
                    this.$message({
                        message: res.data.msg,
                        type: res.data.code === 200 ? 'success' : 'error',
                        onClose: _ => {
                            if (res.data.code !== 0) {
                                this.doLoadRows()
                            }
                        }
                    })
                })
            }
            // 编辑
            ,doAdd ( item ) {
                if ( item != undefined ) {
                    this.formData = item;
                } else {
                    this.formData = {
                        id: 0,
                        title: '',
                        status: 1,
                        type: 1,
                        icon: ''
                    }
                }
                this.dialogFormVisible = true
            }
            ,closeDialog(){
                this.$refs['formData'].clearValidate();
            }
            ,onSubmit (formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post( Url({a:'save'}) , this.formData).then(res => {
                            this.$message.success('操作成功');
                            this.dialogFormVisible = false;
                            this.doLoadRows(1)
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
            ,doDelete (id) {
                this.$confirm('确认删除该条记录?').then(_ => {
                    axios.get(Url({a:'delete' , q:{id:id}})).then(res => {
                        this.$message.success('操作成功');
                    })
                    return this.doLoadRows();
                }).catch(_ => {})
            }
			,doRoutes(recomandId) {
                this.showDoRoutes = true
                this.relRoutesWhere.recomand_id = recomandId

                this.getRoutesList()
                this.getRelRoutesList()
			}
            ,doLoadRoutes(pn) {
                this.showDoRoutes = true
            }
            ,addRelRoutes(routeId) {
                if ( !this.relRoutesWhere.recomand_id ) {
                    return this.$message.error('推荐ID不能为空')
                }
                axios.post(Url({c:'maintain.rel_recomand_routes', a:'save'}) , {recomand_id:this.relRoutesWhere.recomand_id, route_id: routeId }).then(res => {
                    if ( res.data.code == 200 ) {
                        this.$message.success('推荐成功');
                        this.initRelRoutesWhere()
                        this.getRelRoutesList(1)
                    } else {
                        return this.$message.error(res.data.msg)
                    }
                })
            }
            ,rmRelRoutes(routeId) {
                if ( !this.relRoutesWhere.recomand_id ) {
                    return this.$message.error('推荐ID不能为空')
                }
                this.$confirm('确认移除该线路关联吗?').then(_ => {
                    axios.post(Url({c:'maintain.rel_recomand_routes', a:'remove'}) , {recomand_id:this.relRoutesWhere.recomand_id, route_id: routeId }).then(res => {
                        if ( res.data.code == 200 ) {
                            this.$message.success('取消推荐成功');
                            this.initRelRoutesWhere()
                            this.getRelRoutesList(1)
                        } else {
                            return this.$message.error(res.data.msg)
                        }
                    })
                }).catch(_ => {})
            }
			,closeRoutes() {
                this.routesWhere.route_name = ''
                this.routesWhere.page = 1
            }
            ,initRelRoutesWhere() {
                this.relRoutesWhere.route_name = ''
                this.relRoutesWhere.page = 1
            }
            ,changeRoutesSort(evt) {
                axios.post(Url({c:'maintain.rel_recomand_routes', a:'set_sort'}) , evt).then(res => {
                    if ( res.data.code == 200 ) {
                        this.$message.success('排序成功');
                        this.initRelRoutesWhere()
                        this.getRelRoutesList(1)
                    } else {
                        return this.$message.error(res.data.msg)
                    }
                }).catch(_ => {
                    this.$message.error('操作失败')
                })
            }
            ,getRoutesList(pn) {
                this.routesWhere.page = pn || 1
                this.routeloading = true
                axios.get(Url({c:'routes.routes',a:'get_list' , q:this.routesWhere})).then(res => {
                    this.routes = res.data.data
                    this.routesCount = res.data.count
                    this.routeloading = false
                })
            }
            ,getRelRoutesList(pn) {
                this.relRoutesWhere.page = pn || 1
                this.relRouteloading = true
                axios.get(Url({c:'maintain.rel_recomand_routes',a:'get_list' , q:this.relRoutesWhere})).then(res => {
                    this.relRoutes = res.data.data
                    this.relRoutesCount = res.data.count
                    this.relRouteloading = false
                })
            }
        },
        mounted(){
            this.doLoadRows()
        }
    })
</script>
{/block}